/*
 * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2019. All rights reserved.
 * Description: app uart demo
 * Author: Hisilicon
 * Create: 2019-12-09
 */

#include <app_demo_uart.h>

#include "oled_demo.h"

hi_u32 g_uart_demo_task_id = 0;
hi_u8 SEND_BUFF[2] = {0X8A,0X8A};

hi_u8 Get_XL;//心率
hi_u8 Get_XY;//血氧
hi_u8 Get_GY;//高压
hi_u8 Get_DY;//低压
hi_u8 sta_con_flag;

static hi_void *uart_demo_task(hi_void *param)
{
    hi_u8 uart_buff[UART_BUFF_SIZE] = {0};
    hi_u8 *uart_buff_ptr = uart_buff;
    hi_unref_param(param);
    printf("Initialize uart demo successfully, please enter some datas via DEMO_UART_NUM port...\n");

    for (;;) {
        hi_s32 len = hi_uart_read(DEMO_UART_NUM, uart_buff_ptr, UART_BUFF_SIZE);
        if (len > 0) {
#ifdef WRITE_BY_INT
           if(len == 12)
           {
               //hi_uart_write(DEMO_UART_NUM, uart_buff_ptr, len);
               Get_GY = uart_buff_ptr[7];
               Get_DY = uart_buff_ptr[8];
               Get_XL = uart_buff_ptr[1];
               Get_XY = uart_buff_ptr[2];
               	//OLED_Clear();
	OLED_ShowChar(0,0,(unsigned char)(Get_DY / 100 + '0'),16);
	OLED_ShowChar(8,0,(unsigned char)(Get_DY / 10 % 10 + '0'),16);
	OLED_ShowChar(16,0,(unsigned char)(Get_DY % 10 + '0'),16);

	OLED_ShowChar(40,0,(unsigned char)(Get_GY / 100 + '0'),16);
	OLED_ShowChar(48,0,(unsigned char)(Get_GY / 10 % 10 + '0'),16);
	OLED_ShowChar(56,0,(unsigned char)(Get_GY % 10 + '0'),16);

	OLED_ShowChar(0,20,(unsigned char)(Get_XL / 100 + '0'),16);
	OLED_ShowChar(8,20,(unsigned char)(Get_XL / 10 % 10 + '0'),16);
	OLED_ShowChar(16,20,(unsigned char)(Get_XL % 10 + '0'),16);


	OLED_ShowChar(40,20,(unsigned char)(Get_XY / 100 + '0'),16);
	OLED_ShowChar(48,20,(unsigned char)(Get_XY / 10 % 10 + '0'),16);
	OLED_ShowChar(56,20,(unsigned char)(Get_XY % 10 + '0'),16);
	OLED_Refresh();
    sta_con_flag = 2;
               printf("\r\n%d %d %d %d\r\n",uart_buff_ptr[7],uart_buff_ptr[8],uart_buff_ptr[1],uart_buff_ptr[2]);
           }


#else
            hi_uart_write_immediately(DEMO_UART_NUM, uart_buff_ptr, len);
#endif
        } else {
            printf("Read nothing!\n");
            hi_sleep(1000); /* sleep 1000ms */
        }
    }

    hi_task_delete(g_uart_demo_task_id);
    g_uart_demo_task_id = 0;

    return HI_NULL;
}

/*
 * This demo simply shows how to read datas from UART2 port and then echo back.
 */
hi_void uart_demo(hi_void)
{
    hi_u32 ret;
    hi_uart_attribute uart_attr = {
        .baud_rate = 38400, /* baud_rate: 115200 */
        .data_bits = 8,      /* data_bits: 8bits */
        .stop_bits = 1,
        .parity = 0,
    };

    /* Initialize uart driver */
    ret = hi_uart_init(DEMO_UART_NUM, &uart_attr, HI_NULL);
    if (ret != HI_ERR_SUCCESS) {
        printf("Failed to init uart! Err code = %d\n", ret);
        return;
    }
   
    /* Create a task to handle uart communication */
    hi_task_attr attr = {0};
    attr.stack_size = UART_DEMO_TASK_STAK_SIZE;
    attr.task_prio = UART_DEMO_TASK_PRIORITY;
    attr.task_name = (hi_char*)"uart_demo";
    ret = hi_task_create(&g_uart_demo_task_id, &attr, uart_demo_task, HI_NULL);
    if (ret != HI_ERR_SUCCESS) {
        printf("Falied to create uart demo task!\n");
    }
}
